#include "selectcodecwizardpage.h"
#include "ui_selectcodecwizardpage.h"

#include "encoderprovidermgr.h"
#include "encoderinterface.h"
#include "encoderoptionmgr.h"
#include "settingwidgetinterface.h"

#include <QComboBox>
#include <QVariant>
#include <QRadioButton>
#include <QButtonGroup>
#include <QSpacerItem>
#include <QSettings>

#include "ldebug.h"

#define SIZE_BASE_ON_WIDTH  0
#define SIZE_BASE_ON_HEIGHT 1

SelectCodecWizardPage::SelectCodecWizardPage(QWidget *parent) :
    QWizardPage(parent),
    ui(new Ui::SelectCodecWizardPage)
{
    ui->setupUi(this);

    QSettings *s = new QSettings(this);


    m_option = EncoderOptionMgr::instance()->createOption(s);
    setupEncoderList();
}

SelectCodecWizardPage::~SelectCodecWizardPage()
{
    delete ui;
}

void SelectCodecWizardPage::setupEncoderList() {
    EncoderInterface::Infomation info;
    QVariant data;

    QList<EncoderInterface *> encoders =
	    EncoderProviderMgr::instance()->allAvailableEncoderProviders();
    foreach (EncoderInterface *iface, encoders) {
	info = iface->infomation();
	data.setValue((void *)iface);

	ui->cbEncoderList->addItem(info.displayName,data);
    }
}

void SelectCodecWizardPage::on_cbEncoderList_currentIndexChanged(int index)
{
    QVariant data = ui->cbEncoderList->itemData(index);
    EncoderInterface * interface =
	    (EncoderInterface *)(data.value<void *>());
    if (!interface){
	DBG->error("Can't find a valid encoder for combo option\n");
	return;
    }

    SettingWidgetInterface *w = interface->configWidget(m_option);
    w->setParent(this);
    EncoderInterface::Infomation info = interface->infomation();
    if (m_configWidgets.contains(info.name)) {
	// Do nothing
    }else{
	m_configWidgets.insert(info.name,w);
	ui->selectCodecLayout->insertWidget(1,w,1);
    }

    ui->labelEncoderHelp->setText(info.intro);

    showEncoderConfigWidget(info.name);
}

void SelectCodecWizardPage::showEncoderConfigWidget(const QString &key) {
    QWidget *to_show = m_configWidgets.value(key,NULL);

    foreach (QWidget *w, m_configWidgets) {
	w->hide();
    }

    if (to_show) to_show->show();
}

